﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows;
using WPFExcelReport.Chart;
namespace WPFExcelReport
{
    public partial class TSTEPQTY
    {
        #region ==AUF===========
        public static readonly DependencyProperty AUFProperty = DependencyProperty.Register("AUF",
            typeof(string), typeof(TSTEPQTY),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnAUFChanged));
        public string AUF
        {
            get
            {
                return (string)GetValue(AUFProperty);
            }
            set
            {
                SetValue(AUFProperty, value);
            }
        }
        private static void OnAUFChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSTEPQTY;
            if (src != null)
            {
                src.AUF = (string)e.NewValue;
                src.ResetChart();
            }
        }
        #endregion
        public TSTEPQTY()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_MKRF(); 
            DataContext = MV;
            MESSAGES += "BBBS13;GBBS13;WBBS13;"; 
            InitializeComponent();
        }
        
        public override void OnWorkflowIDChange(ReflectionStudio.Core.Events.MessageEventArgs e)
        {
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            {
                case "BBBS13":
                case "GBBS13":
                case "WBBS13":
                    DataRow dwData = TBMain.Rows.Add();
                    dwData["MACHINEID"] = msg.KRS + msg.LFD + msg.POS;
                    dwData["ABFSOLLMG"] = msg.P1;
                    dwData["ABFSOLLDAT"] = msg.P2;
                    dwData["ABFAGANG"] = msg.P3;
                    dwData["ABFBASMGE"] = msg.P4; 
                    SetUpChart05();
                    SetUpChart06();
                    break;
            }
        }
       
        public void SetUpChart05()
        {
            Chart05.SetChartSleep();
            /*dwData["ABFSOLLDAT"] = row["GABBUCHDAT"];
            dwData["ABFSOLLMG"] = row["GABKG"]*/
            ;
            int idx = 1;
            string X = "ABFSOLLDAT";
            string Y = "ABFSOLLMG";
            string name = "";
            //ABFAGANG：归类。Chart05.SetUp自动将日期累加

            var query = (from DataRow row in TBMain.AsEnumerable()
                         orderby row.Field<string>("ABFAGANG")
                         select row.Field<string>("ABFAGANG")).Distinct();
            foreach (var str in query)
            {
                name = Helpers.Helper.Right(str.ToString(), 2);
                X = "ABFSOLLDAT";
                Y = "ABFSOLLMG";
                string sort = " ABFSOLLDAT ASC";
                DataRow[] rows = TBMain.Select("ABFAGANG='" + str + "'", sort);
                DataTable temp = rows.CopyToDataTable();
                Chart05.RATIO = 1;
                Chart05.Converter = 1;
                Chart05.SetUp(idx, name, temp, X, Y);
                idx++;
            }
            //Chart05.Confirm();
            Chart05.SetChartGetUp();
         }
        public void SetUpChart06()
        {

            /*dwData["ABFSOLLDAT"] = row["GABBUCHDAT"];
            dwData["ABFSOLLMG"] = row["GABKG"]*/
            ;
            int idx = 1;
            string X = "ABFSOLLDAT";
            string Y = "ABFSOLLMG";
            string name = "";

            X = "ABFAGANG";
            Y = "ABFSOLLMG";
            Chart06.SetChartSleep();
            var qData = from XD in TBMain.AsEnumerable()
                        group XD by new
                        {
                            KEY = XD[X].ToString(),
                        }
                            into g
                            select new
                            {
                                KEY = g.Key,
                                YQTY = g.Sum(XD => Convert.ToDouble(XD[Y].ToString())),
                                TargetRatio = g.Max(XD => Convert.ToDouble( Helpers.Helper.Left(XD["ABFBASMGE"].ToString()+"00000000",8))),
                            };
            double MQTY = 0;//分盘量
            double YQTY = 0;//细纱产量
            double TargetRatio = 0;//计划部设定的投料比


            foreach (var XD in qData)
            {
                string AGANG = (string)XD.KEY.KEY;
                if (AGANG.StartsWith("1-"))
                    MQTY = XD.YQTY;
                if (AGANG.StartsWith("2-"))
                    TargetRatio = XD.TargetRatio / 10000000;
                if (AGANG.StartsWith("3-"))
                    YQTY = XD.YQTY;
                if (AGANG.StartsWith("4-"))
                    YQTY = XD.YQTY;
            }
            if (YQTY == 0)
                YQTY = MQTY * 1;
             if (TargetRatio == 0)
                return;
            if (MQTY == 0)
                return;
            Chart06.SetChartSleep();
            Chart06.vm.DATA1.Clear();

            name = "目标:" + Helpers.Helper.RoundX(TargetRatio, 1000) + "/实际:" + Helpers.Helper.RoundX(MQTY / YQTY, 1000);
            Chart06.RATIO = 1;
            Chart06.Converter = 1;
            Chart06.vm.S1 = name;
            Chart06.vm.SeriesData1.SeriesDisplayName = name;
            Chart06.vm.ChartTitle1 = name;
            Chart06.vm.ChartSubTitle1 = name;
            Chart06.vm.DefaultTitle = name;
            ChartClass ChartClass = new ChartClass();
            ChartClass.Category = "投料比";
            ChartClass.Number = +Helpers.Helper.RoundX(TargetRatio * 100 / (MQTY / YQTY), 100);//最终显示的150%的来源
            Chart06.vm.DATA1.Add(ChartClass);
            Chart06.SetChartGetUp(); 
        }
        public void ResetChart()
        {
            if (AUF == null)
                return;
            if (AUF.Length < 2)
                return;
            if (TBMain == null)
                return;
            TBMain.Clear();
            Chart05.ResetChart();
            Chart06.ResetChart();
        }
        private void btnShowChart_Click(object sender, RoutedEventArgs e)
        {
            SetUpChart05();
            SetUpChart06();
        }
    }
}